var express = require('express');
var router = express.Router();

var LMenu = require('../schema/LMenu'),
    rename = false;

function deleteMenu(id) {
    LMenu.find({
        pre_id: id
    }, function(err, menus) {
        for (var i = 0; i < menus.length; i++) {
            deleteMenu(menus[i]._id);
        }
        LMenu.remove({
            _id: id
        }, function(err) {
            return;
        });
    })
};


//通过当前用户；当前类型；上级菜单名字，查找上级菜单_id 
function findParent(req, res) {
    var pre_id = '';
    LMenu.findOne({
        M_name: req.body.pre_name,
        M_rank: (req.body.rank - 1),
        user: req.cookies.user.trim(),
        module: '1' //先写死一个默认值
    }, function(err, menu) {
        if (err) {
            console.log(err);
        } else if (menu) { //如果数据库中存在这个菜单
            pre_id = menu._id
        }
        is_exit(req, res, pre_id);
    });
}

//上级菜单中有没有同名的孩子
function is_exit(req, res, pre_id) {
    LMenu.findOne({
        M_name: req.body.name,
        pre_id: pre_id,
        user: req.cookies.user.trim(),
        module: '1' //先写死一个默认值
    }, function(err, menu) {
        if (err) {
            console.log(err);
        }
        if (menu) {
            res.send({
                stutas: '1',
                err: '该菜单栏已存在'
            });
        } else {
            rename && updata(req, res);
            !rename && save(req, res, pre_id);
        }
    })
}

function updata(req, res) {
    LMenu.findOne({
        M_name: req.body.old_name,
        M_rank: req.body.rank,
        user: req.cookies.user.trim(),
        module: '1'
    }, function(err, menu) {
        if (err) {
            res.send('数据库修复中，请稍后再试');
        } else {
            menu.M_name = req.body.name;
            menu.save(function(err, menu) {
                if (err) {
                    res.send('数据库修复中，请稍后再试');
                } else {
                    res.send('1');
                    rename = false;
                }
            })
        }
    })
}

function save(req, res, pre_id) {
    var m = new LMenu({
        M_name: req.body.name,
        M_rank: req.body.rank,
        pre_id: pre_id,
        user: req.cookies.user.trim(),
        module: '1'
    });
    m.save(function(err, menu) {
        if (err) {
            console.log(err);
        } else if (menu) {
            res.send({
                stutas: '0',
                name: menu.M_name,
                id: menu._id,
                rank: menu.M_rank
            });
        }
    });
}

//delete menu 
router.post('/menu/deleted', function(req, res, next) {
    LMenu.findOne({
        M_name: req.body.name,
        M_rank: req.body.rank,
        user: req.cookies.user.trim(),
        module: '1'
    }, function(err, menu) {
        deleteMenu(menu._id);
        res.send('1');
    });
});

//rename menu 
router.post('/menu/rename', function(req, res, next) {
    rename = true;
    LMenu.findOne({
        M_name: req.body.name,
        M_rank: req.body.rank,
        user: req.cookies.user.trim(),
        module: '1'
    }, function(err, menu) {
        findParent(req, res);
    });
});

//add menu
router.post('/menu/add', function(req, res, next) {
    findParent(req, res);
})



module.exports = router;